<?php


namespace app\blog\service;


class MessageService
{
    public function MessageList(array $arr, int $id = -1): array {
        return $this->getMessageList($arr, $id);
    }

    // 递归获取无限极分类
    private function getMessageList(array $arr, $parent_id): array {
        $tree = array();
        foreach ($arr as $k => $v) {
            if ($v["parent_id"] == $parent_id) {
                unset($arr[$k]);
                if (!empty($arr)) {
                    $children = $this->getMessageList($arr, $v["id"]);
                    if (!empty($children)) {
                        $v["children"] = $children;
                    }
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }


}